import br.com.nooclix.salon.ui.dto.Employee.EmployeeTO;
import br.com.nooclix.salon.ui.dto.attendance.AttendanceSheetTO;
import br.com.nooclix.salon.ui.dto.attendance.AttendanceTO;
import br.com.nooclix.salon.ui.dto.product.ProductTO;
import br.com.nooclix.salon.ui.dto.service.ServiceTO;
import br.com.nooclix.salon.ui.screen.attendancesheet.AttendanceSheetWindow;
import br.com.nooclix.salon.ui.util.ResponseData;
import br.com.nooclix.salon.ui.util.Utilitys;
import br.com.nooclix.salon.ui.util.WindowsMessage;

import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.TextInput;
import mx.events.DropdownEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public var attendanceCreated:AttendanceTO;
public var employee:ArrayCollection;
public var attendanceSheet:AttendanceSheetTO;

public function start():void{
	findEmployeeByName();
	if(this.currentState == "service"){
		findServiceByName();
	}else{
		findProductByName();
	}
}

public function findServiceByNameResult(event:ResultEvent):void{
	var responseData:ResponseData = event.result as ResponseData;
	
	if(responseData.status == ResponseData.ok()){
		cbService.dataProvider = responseData.items;
		cbService.labelField = "name";
		cbService.dispatchEvent(new DropdownEvent(DropdownEvent.CLOSE));
		
	}else{
		WindowsMessage.alarm(responseData.message);
	}
}

public function findEmployeeByNameResult(event:ResultEvent):void{
	var responseData:ResponseData = event.result as ResponseData;
	
	if(responseData.status == ResponseData.ok()){
		cbEmployee.dataProvider = responseData.items;
	}else{
		WindowsMessage.alarm(responseData.message);
	}
}

public function findProductByNameResult(event:ResultEvent):void{
	var responseData:ResponseData = event.result as ResponseData;
	
	if(responseData.status == ResponseData.ok()){
		cbProduct.dataProvider = responseData.items;
		cbProduct.labelField = "name";
		cbProduct.dispatchEvent(new DropdownEvent(DropdownEvent.CLOSE));
	}else{
		WindowsMessage.alarm(responseData.message);
	}
}
public function onSelectSevice():void{
	var service:ServiceTO = cbService.selectedItem as ServiceTO;
	fieldCodeService.text = service.code.toString();
	fieldValue.text = Utilitys.formatCurrencyBR(service.value);
	
}
public function onSelectProduct():void{
	var product:ProductTO = cbProduct.selectedItem as ProductTO;
	fieldCodeProduct.text = product.code.toString();
	fieldValue.text = Utilitys.formatCurrencyBR(product.value);
	
}

private function cbEmployeeLabelFunction(item:Object):String {
	var e:EmployeeTO = item as EmployeeTO;
	return e.person.name;
}

private function cbProductLabelFunction(item:Object):String{
	var p:ProductTO = item as ProductTO;
	return p.name;
}
public function closeWindow():void{
	PopUpManager.removePopUp(this);
}

public function addAttendanceClick():void{
	var e:Event = new Event("ClosePopUpAttendance");
	this.dispatchEvent(e);
	closeWindow();
	
	
}

public function getAttendanceSelected():AttendanceTO{
	
	if(attendanceCreated == null){
		attendanceCreated = new AttendanceTO();
	}
	
	//Altera o valor do combobox wm ChooseAttendance.mxml
	attendanceCreated.employee = cbEmployee.selectedItem as EmployeeTO;
	attendanceCreated.quantity = Number(fieldQuant.text);
	if(this.currentState == "service"){
		attendanceCreated.service = cbService.selectedItem as ServiceTO;
		attendanceCreated.product = null;
		//attendanceSheet.verifyIfEquals(attendanceCreated);
		//Alert.show("Adicionou servico "+ attendanceCreated.service);
	}else{
		attendanceCreated.service = null;
		attendanceCreated.product = cbProduct.selectedItem as ProductTO;
		//attendanceSheet.verifyIfEquals(attendanceCreated);
		//Alert.show("Adicionou produto " +attendanceCreated.product);
	}
	
	return attendanceCreated; 
}

private function findServiceByName():void{
	serviceRemote.findByName(fieldServiceSearch.text);
	
}

private function findProductByName():void{
	productRemote.findByName(fieldProductSearch.text);
}

private function findEmployeeByName():void{
	
	employeeRemote.findByName(fieldEmployeeSearch.text);
	
}

public function format(tf:TextInput):void{
	
	var temp:Number=Number(tf.text);
	
	tf.text = Utilitys.formatCurrencyBR(temp);
	
}

public function unformat(tf:TextInput):void{
	
	tf.text = Utilitys.unformat(tf.text);
	
}


private function fault(event:FaultEvent):void
{
	WindowsMessage.error(event.message.toString());
}

